package web

import (
	"context"

	"portal/internal/errorx"
	"portal/internal/svc"
	"portal/internal/types"
	"portal/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListNewsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
	newsmodel model.AwNewsModel
}

func NewListNewsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListNewsLogic {
	return &ListNewsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
		newsmodel: model.NewAwNewsModel(svcCtx.Orm),
	}
}

func (l *ListNewsLogic) ListNews(req *types.ListNewsRequest) (resp *types.CommonResponse, err error) {
	// todo: add your logic here and delete this line
	news := &types.News{
		PublishTime: req.SelectDate,
		Type: req.ActiveId,
	}
	page := &types.PageParamRequest{
		PageSize: req.PageSize,
		PageNum: req.PageNum,
	}
	newListDto,err := l.newsmodel.GetListByCondition(l.ctx,nil,*page,news)
	if err != nil {
		return nil,errorx.NewBizError(500,err.Error())
	}
	newListVo := make([]*types.NewsVo,0)
	for _,val := range newListDto {
		newListVo = append(newListVo, &types.NewsVo{
			NewsId: val.NewsId,
			CoverImg: val.CoverImg.String,
			NewsTitle: val.NewsTitle,
			NewsDesc: val.NewsDesc,
			NewsContent: val.NewsContent.String,
			NewsPath: val.NewsPath.String,
			Typename: val.Typename,
			PublishTime: val.PublishTime,
			UpdateTime: val.UpdateTime,
		})
	}
	total, err := l.newsmodel.GetCount(l.ctx, nil, news)
	if err != nil {
		return nil, errorx.NewBizError(500, err.Error())
	}
	limit := int64(len(newListVo)) + (req.PageNum-1) *req.PageSize

	return &types.CommonResponse{
		Status: 200,
		Msg: "查询成功",
		Data: &types.ListNewsResponse{
			News: newListVo,
			PageParamResponse: types.PageParamResponse{
				Total: total,
				Size: limit,
			},
		},
	},nil
}
